//
//  FindViewController.swift
//  约健
//
//  Created by tyh on 2017/10/16.
//  Copyright © 2017年 tyh. All rights reserved.
//  ,UICollectionViewLayout

import UIKit

class FindViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    @IBOutlet weak var Select: UISegmentedControl!

    @IBOutlet weak var CollectionView: UICollectionView!
    
    
    @IBAction func Changed(_ sender: UISegmentedControl) {
        switch Select.selectedSegmentIndex {
        case 0:
            gethuodong()
        case 1:
            getdashi()
        case 2:
            
            getshaitu()
        default:
            break

        }
        
    }
    
    var turebarbtnItem:UIBarButtonItem!
    var rightBtn:UIButton = UIButton.init()
    var DaShiList = NSArray()
    var HuoDongList = NSArray()
    var ShaiTuList = NSArray()
    override func viewDidLoad() {
      /*
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.isTranslucent = true
     
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.darkGray]
        self.navigationController?.navigationBar.tintColor = UIColor.darkGray
     */
        rightBtn.sizeToFit()
        rightBtn.addTarget(self, action: #selector(ToWrite), for: UIControlEvents.touchUpInside)
        turebarbtnItem = UIBarButtonItem.init(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = turebarbtnItem
        self.rightBtn.setImage(UIImage.init(named: "写.png"), for: .normal)
        automaticallyAdjustsScrollViewInsets = false
        getdashi()
        Select.selectedSegmentIndex = 1
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: (width-20)/2, height: height * 0.4)
        layout.sectionInset = UIEdgeInsets.init(top: 20, left: 5, bottom: 0, right: 5)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing =  15
        
        
        CollectionView.delegate = self
        CollectionView.dataSource = self

        CollectionView.register(UINib.init(nibName: "MainDisplayCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")
        
         CollectionView.register(UINib.init(nibName: "ShaiTuCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "shaitucell")
        
          CollectionView.register(UINib.init(nibName: "HuodongCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "huodongcell")
        
        CollectionView.collectionViewLayout = layout
    
        


        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
   
        // Dispose of any resources that can be recreated.
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if Select.selectedSegmentIndex == 1{
        if DaShiList.count != 0{
            return DaShiList.count
        }
        }else if Select.selectedSegmentIndex == 0{
            if HuoDongList.count != 0{
                return HuoDongList.count
            }
        }else if Select.selectedSegmentIndex == 2{
            if ShaiTuList.count != 0{
                return ShaiTuList.count
            }
        }
    return 0
        
    }
    //最小item间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{
        return 0
    }
    
    //最小行间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        if Select.selectedSegmentIndex == 0{
            return 10
        }
        return 20
    }
    //items尺寸
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if Select.selectedSegmentIndex == 1{
        return CGSize(width: width*2/3, height: height * 0.4)
        }else if Select.selectedSegmentIndex == 2{
            return CGSize(width: (width-20)/2, height: height * 0.3)
        }else if Select.selectedSegmentIndex == 0{
            return CGSize(width: width, height: height * 0.25)
        }
        return CGSize(width: 0, height: 0)
    }
    //偏移量
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(5, 5, 0, 5)
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        var basecell = UICollectionViewCell()
        
        if Select.selectedSegmentIndex == 1{
            let cell:MainDisplayCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MainDisplayCollectionViewCell

        
        let model:DaShiModel = DaShiList[indexPath.row] as! DaShiModel
            
        let str0 = "大师名:" + "\(model.realname!)"

        cell.DaShiName.text = str0
    
        let imageStr = "http://172.24.10.175/workout/Uploads/" + (model.thumb! as String)
        let imageUrl = URL(string: imageStr)
        let data = try! Data(contentsOf: imageUrl!)
        let image = UIImage(data: data)
        
        cell.Pic.image = image
        
        let address = "\(model.province_name!)" + "\(model.city_name!)"
        cell.Address.text = address
            
            if model.club_name == nil{
                cell.Club_name.text = "#不属于俱乐部#"
            }else{
                let str = "#" + "\(model.club_name as String)" + "#"
                cell.Club_name.text = str
            }
            
        cell.hits.text = model.hits as String
            
        cell.Text.text = model.introduction as String
            
        let str1 = "擅长:" + "\(model.actcate_name!)"
            
        cell.DaShiJiNeng.text = str1
            
        basecell = cell
            
        }
        
        else if Select.selectedSegmentIndex == 0{
            let cell:HuodongCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "huodongcell", for: indexPath) as! HuodongCollectionViewCell

           
            let model:HuoDongModel = HuoDongList[indexPath.row] as! HuoDongModel
            cell.ActName.text = model.rec_name as String
            cell.StartTime.text = model.update_time as String
            cell.Hits.text = model.hits as String
            var str = "闲暇时光"
         
            if model.rec_mod == "activity"{
                str = "竞技比赛"
            }else if model.rec_mod == "master"{
                str = "大型讲座"
            }
            cell.ActType.text = str
            let imageStr = "http://172.24.10.175/workout/Uploads/" + (model.thumb! as String)
            let imageUrl = URL(string: imageStr)
            let data = try! Data(contentsOf: imageUrl!)
            let image = UIImage(data: data)
            
            cell.Pic.image = image
          
 
            
            basecell = cell
        }
        else{
            let cell:ShaiTuCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "shaitucell", for: indexPath) as! ShaiTuCollectionViewCell
            
            let model:ShaiTuModel = ShaiTuList[indexPath.row] as! ShaiTuModel
            /*
            let ads = model.shaier[key:"photo"] as? String
            let imageStr1 = "http://172.24.10.175/workout/Uploads/" + "\(String(describing: ads))"
            let imageUrl1 = URL(string: imageStr1)
            let data1 = try! Data(contentsOf: imageUrl1!)
            let image1 = UIImage(data: data1)
            if image1 == nil{
                cell.TouXIang.image = UIImage(named: "TX.png")
            }else{
 */
            cell.TouXIang.image = UIImage(named: "TX .png")
            
            let imageStr = "http://172.24.10.175/workout/Uploads/" + (model.thumb0! as String)
            let imageUrl = URL(string: imageStr)
            let data = try! Data(contentsOf: imageUrl!)
            let image = UIImage(data: data)
            
            cell.Pic.image = image
            cell.UserName.text = model.shaier[key:"user_name"] as? String
            cell.Hits.text = model.hits as String
            
            if model.title == ""{
                cell.Text.text = "该用户很懒..."
            }else{
                cell.Text.text = model.title as String
            }
            
            if model.club_name == nil{
                cell.ClubName.text = "#未加入俱乐部#"
            }else{
                let str = "#" + "\(model.club_name as String)" + "#"
                cell.ClubName.text = str
            }
            
            
            basecell = cell
        }
        
        
        return basecell
        
        
    }
    func getdashi(){
        let parameters = ["mod":"master","SessionID":"2ul9sj8vlfofj122e1s98048k2","page":1] as NSDictionary
        NetWorkMngTool.sharedNetworkMngtool().Network_GetDaShiList(parameters, view: self.view){(dashiList) in
            self.DaShiList = dashiList
            print(self.DaShiList)
            self.CollectionView.reloadData()
        }
    }
    func gethuodong(){
        
        let parameters = ["mod":"acmrecom","SessionID":"2ul9sj8vlfofj122e1s98048k2","page":1] as NSDictionary
        NetWorkMngTool.sharedNetworkMngtool().Network_GetHuoDongList(parameters, view: self.view){(huodongList) in
            self.HuoDongList = huodongList
            print(self.HuoDongList)
            self.CollectionView.reloadData()
        }
        
    }
    func getshaitu(){
        
        let parameters = ["mod":"shaitu","SessionID":"2ul9sj8vlfofj122e1s98048k2","related":1] as NSDictionary
        NetWorkMngTool.sharedNetworkMngtool().Network_GetShaiTuList(parameters, view: self.view){(shaituList) in
            self.ShaiTuList = shaituList
            print(self.ShaiTuList)
            self.CollectionView.reloadData()
        }
        
    }
    
    func ToWrite(){
       self.performSegue(withIdentifier: "ToWrite", sender: nil)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
